extends Node2D


@onready var sprite: Sprite2D = $Sprite2D


var selector: BehaviorTreeSelector
var done: bool


func _ready():
    var node1 := TestSelectorNode.new("fail_node", BehaviorTreeNode.NodeState.FAILURE, 0.3)

    var node2 := TestSelectorNode.new("success_node", BehaviorTreeNode.NodeState.SUCCESS, 0.2)

    selector = BehaviorTreeSelector.new()
    selector.add_child(node2)
    selector.add_child(node1)

    selector._on_enter()


func _process(delta: float):
    if done:
        return
    var state := selector._tick(delta)
    if state == BehaviorTreeNode.NodeState.SUCCESS:
        selector._on_exit()
        done = true
    elif state == BehaviorTreeNode.NodeState.FAILURE:
        selector._on_exit()
        done = true


class TestSelectorNode extends BehaviorTreeNode:
    var node_name: String
    var expect_state: NodeState
    var timeout: float

    func _init(name: String, state: NodeState, time: float) -> void:
        node_name = name
        expect_state = state
        timeout = time

    func _on_enter():
        print("enter name: ", node_name)

    func _on_exit():
        print("exit name: ", node_name)

    func _tick(delta: float) -> NodeState:
        timeout -= delta
        if timeout < 0.0:
            return expect_state
        else:
            return NodeState.RUNNING
